package cn.lizhangqu.kb.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;

import cn.lizhangqu.kb.R;
import cn.lizhangqu.kb.model.Score;

/**
 * Created by 柯尊诚 on 2015/11/5.
 * kzc
 */
public class ScoreExpandableListAdapter extends BaseExpandableListAdapter{

    private Context context;

    private List<Score> scoreList;

    public ScoreExpandableListAdapter(Context context, List<Score> scoreList) {
        this.context = context;
        this.scoreList = scoreList;
    }

    @Override
    public int getGroupCount() {
        return scoreList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Score getGroup(int groupPosition) {
        return scoreList.get(groupPosition);
    }

    @Override
    public Score getChild(int groupPosition, int childPosition) {
        return scoreList.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    /**
     *  父节点
     * @param groupPosition
     * @param isExpanded
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_child_score, null);
        }
        TextView scoreNameTv = (TextView) convertView.findViewById(R.id.score_child_name);
        TextView scoreValueTv = (TextView) convertView.findViewById(R.id.score_child_value);


        Score score = scoreList.get(groupPosition);

        scoreNameTv.setText(score.getCourseName());
        scoreValueTv.setText(score.getScoreValue());

        return convertView;

    }

    /**
     * 子节点视图
     * @param groupPosition
     * @param childPosition
     * @param isLastChild
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_group_score, null);
        }

        TextView scoreCodeTv = (TextView) convertView.findViewById(R.id.score_group_code);
        TextView scoreNameTv = (TextView) convertView.findViewById(R.id.score_group_name);
        TextView scoreClassTv = (TextView) convertView.findViewById(R.id.score_group_class);
        TextView scoreCreditsTv = (TextView) convertView.findViewById(R.id.score_group_credits);
        TextView scoreGradePointTv = (TextView) convertView.findViewById(R.id.score_group_gradepoint);
        TextView scoreValueTv = (TextView) convertView.findViewById(R.id.score_group_value);
        Log.d("posion", groupPosition + "  <====>  " + childPosition);
        Score score = scoreList.get(groupPosition);
        scoreCodeTv.setText(score.getCourseCode());
        scoreNameTv.setText(score.getCourseName());
        scoreClassTv.setText(score.getCourseClass());
        scoreCreditsTv.setText(score.getCredits());
        scoreGradePointTv.setText(score.getGradePoint());
        scoreValueTv.setText(score.getScoreValue());

        return convertView;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
